package com.newbsd.web.maventest.Filter;

import java.io.IOException;
import java.util.regex.Pattern;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.newbsd.web.maventest.util.HttpUtil;



/**
 * 
 * @author 张成玉--设置拦截器
 *
 */
public class LoginFilter implements Filter{

	private String exclude = null;
	private String auth = null;
	private String css = null;
	private String js = null;
	private String images = null;
	private String login = null;
	private String register = null;
	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		exclude = filterConfig.getInitParameter("exclude");
		auth = filterConfig.getInitParameter("auth");
		css = filterConfig.getInitParameter("css");
		js = filterConfig.getInitParameter("js");
		images = filterConfig.getInitParameter("images");
		login = filterConfig.getInitParameter("login");
		register = filterConfig.getInitParameter("register");
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		
		HttpServletRequest req = (HttpServletRequest) request;
		HttpServletResponse resp = (HttpServletResponse) response;
		HttpSession session = req.getSession();

		//		System.out.println(req.getRequestURI());
		
		if(exclude.equals(req.getRequestURI())){
			chain.doFilter(request, response);
		}else if(auth.equals(req.getRequestURI())){
			chain.doFilter(request, response);
		}else if(Pattern.matches(css, req.getRequestURI())){ 	// 张成玉css文件放行
			chain.doFilter(request, response);
		}else if(Pattern.matches(js, req.getRequestURI())){		// 张成玉js文件放行
			chain.doFilter(request, response);
		}else if(Pattern.matches(images, req.getRequestURI())){	// 张成玉image文件放行
			chain.doFilter(request, response);
		}else if(Pattern.matches(login, req.getRequestURI())){	// 张成玉login页面放行
			chain.doFilter(request, response);
		}else if(Pattern.matches(register, req.getRequestURI())){	// 张成玉register页面放行
			chain.doFilter(request, response);
		}else if(session.getAttribute("loginInfo")!=null){//张成玉-获取用户的登录姓名为登录欢迎界面服务
			chain.doFilter(request, response);
		}else {
			HttpUtil.forward(req, resp, "login.jsp");//张成玉-实用页面跳转
		}
	}

	@Override
	public void destroy() {
		
	}

}
